package com.ellecity06.cwhiskymall.ui.invite

import androidx.lifecycle.viewModelScope
import com.ellecity06.common.base.BaseMviViewModel
import com.ellecity06.common.ktx.postEvent
import com.ellecity06.cwhiskymall.bean.request.FindMemberInviteRequest
import com.ellecity06.cwhiskymall.bean.request.H5UrlRequest
import com.ellecity06.cwhiskymall.http.request
import com.ellecity06.cwhiskymall.storage.UserStorage
import kotlinx.coroutines.launch

class InviteFriendViewModel : BaseMviViewModel<InviteFriendState, InviteFriendIntent>() {

    override suspend fun collectIntent(it: InviteFriendIntent) {
        when (it) {
            InviteFriendIntent.FetchData -> {
                findInvitedMemberInfo()
                getOtherSetting()
            }
            InviteFriendIntent.LoadMore -> {
                fetchInviteList()
            }
            InviteFriendIntent.Refresh -> {
                pagerHelper.pageIndex = 1
                fetchInviteList()
            }
            InviteFriendIntent.GetRegisterUrl -> {
                getRegisterH5()
            }
            else -> {}
        }
    }

    private fun getRegisterH5() {
        request(api = { getH5RequestUrl(H5UrlRequest("h5_register_url")) }, success = {
            if (it!=null){
                mState.postEvent(InviteFriendState.RegisterH5(it.url?:""))
            }

        })
    }
    private fun getOtherSetting() {
        request(api = { getOtherSetting() }, success = {
            if (it!=null){
                mState.postEvent(InviteFriendState.OtherSettingValue(it.settingData?:"10"))
            }

        })
    }

    /**
     * 分页获取邀请列表
     */
    private fun fetchInviteList() {
        val request = FindMemberInviteRequest(UserStorage.user?.phone ?: "")
        request.pageNum = pagerHelper.pageIndex
        request(api = { findMemberInfoList(request) }, success = {
            if (it?.records != null) {
                if (pagerHelper.pageIndex == 1) {
                    val haseMore = it.records.size >= request.pageSize
                    mState.postEvent(InviteFriendState.RefreshSuccess(it.records, haseMore))
                } else {
                    val haseMore = it.records.size >= request.pageSize
                    mState.postEvent(InviteFriendState.LoadMoreSuccess(it.records, haseMore))
                }
            }

            pagerHelper.pageIndex++

        }, error = {

            viewModelScope.launch {
                mState.postEvent(InviteFriendState.LoadError)
            }

        })
    }


    private fun findInvitedMemberInfo() {
        val phone = UserStorage.user?.phone ?: ""
        request(api = { findInvitedMemberInfo(phone) }, success = {
            if (it != null) {
                val toString = it.invites.toString().replace(",", "  ")
                mState.postEvent(InviteFriendState.MarqueeInfo(toString))
                mState.postEvent(InviteFriendState.MemberInviteList(it.members ?: mutableListOf()))
            }

        })
    }

}
